package com.h1.staffmanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h1.commonutils.R;
import com.h1.staffmanage.entity.CurriculumVitae;
import com.h1.staffmanage.entity.vo.CVQuery;
import com.h1.staffmanage.service.CurriculumVitaeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mengfanyu
 * @since 2022-06-22
 */
@RestController
@CrossOrigin
@RequestMapping("/staffmanage/curriculum-vitae")
public class CurriculumVitaeController {
    @Autowired
    private CurriculumVitaeService curriculumVitaeService;
    @GetMapping("findAllCV")
    public R findAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<CurriculumVitae> list = curriculumVitaeService.list(null);
        return R.ok().data("items",list);
    }
    //2 逻辑删除简历的方法
    @ApiOperation(value = "逻辑删除简历")
    @DeleteMapping("{id}")
    public R removeCV(@ApiParam(name = "id", value = "简历ID", required = true)
                           @PathVariable long id) {
        boolean flag = curriculumVitaeService.removeById(id);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    //3 分页查询简历的方法
    //current 当前页
    //limit 每页记录数
    @GetMapping("pageCV/{current}/{limit}")
    public R pageCV(@PathVariable long current,
                             @PathVariable long limit) {

        Page<CurriculumVitae> pageCV = new Page<>(current,limit);


        curriculumVitaeService.page(pageCV,null);

        long total = pageCV.getTotal();//总记录数
        List<CurriculumVitae> records = pageCV.getRecords(); //数据list集合



        return R.ok().data("total",total).data("rows",records);
    }
    //4 条件查询带分页的方法
    @PostMapping("pageCVCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,
                                  @RequestBody(required = false) CVQuery cvQuery) {
        //创建page对象
        Page<CurriculumVitae> pageCV = new Page<>(current,limit);

        //构建条件
        QueryWrapper<CurriculumVitae> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String begin = cvQuery.getBegin();
        String end = cvQuery.getEnd();
        Integer gender = cvQuery.getGender();
        String mail = cvQuery.getMail();
        String personalskills = cvQuery.getPersonalskills();
        String name = cvQuery.getName();
        String userId = cvQuery.getUserId();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(userId) ){
            wrapper.eq("userId",userId);
        }
        if(!StringUtils.isEmpty(gender)) {
            wrapper.ge("gender",gender);
        }
        if(!StringUtils.isEmpty(mail)) {
            wrapper.ge("mail",mail);
        }
        if(!StringUtils.isEmpty(personalskills)) {
            wrapper.ge("personalskills",personalskills);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        curriculumVitaeService.page(pageCV,wrapper);

        long total = pageCV.getTotal();//总记录数
        List<CurriculumVitae> records = pageCV.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    //添加简历基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCurriculumVitae(@RequestBody CurriculumVitae curriculumVitae) {
        boolean save = curriculumVitaeService.save(curriculumVitae);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }

    }
    //根据简历id进行查询
    @GetMapping("getCV/{id}")
    public R getCV(@PathVariable long id) {
        CurriculumVitae byId = curriculumVitaeService.getById(id);
        return R.ok().data("CVbyId",byId);
    }
    //简历修改功能
    @PostMapping("updateCV")
    public R updateTeacher(@RequestBody CurriculumVitae curriculumVitae) {
        boolean flag = curriculumVitaeService.updateById(curriculumVitae);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    //简历最终发布
    //修改简历状态
    @PostMapping("publishCV/{id}")
    public R publishCourse(@PathVariable long id) {
        CurriculumVitae curriculumVitae = new CurriculumVitae();
        curriculumVitae.setId(id);
        curriculumVitae.setStatus("Normal");//设置课程发布状态
        curriculumVitaeService.updateById(curriculumVitae);
        return R.ok();
    }


}

